<?php
class Mmuser extends CI_Model {
    public function __construct() {
        parent::__construct();

        $this->load->database();
    }

    public function getUser($id = '') {
        if ($this->db->table_exists('tbl_user')) {
            echo 111111;
        }
        $this->db->select('u.id, username, fullname, u.status, g.name');
        $this->db->from('tbl_user u');
        $this->db->join('tbl_user_group g', 'g.id = u.group_id', 'left');
        if($id) {
            $this->db->where('u.id', $id);
            return $this->db->get()->row_array();
        }

        return $this->db->get()->result_array();
    }

    public function delUser($id) {
        $this->db->where('tbl_user.id', $id);
        return $this->db->delete('tbl_user');
    }

    public function processForm ($data, $id='') {
        // do db stuff after the form was submit and validated

        // all validated post values are in $data
        // e.g. $data['username']

        /*
         * NEW!!!
         * uploaded data is not stored in $data['uploads'] anymore
         *
         * uploaded data is now accessible via the upload field's name
         * and contains all upload data in an associative array
         * e.g. $data['file_upload']
         */

        // add custom errors to the form by using
        // $form->add_error('username', 'The Username is not valid');
        $dataToInsert = array(
            'username' => $data['username'],
            'password' => md5($data['password']),
            'group_id' => 3,
            'status' => 2
        );
        $dataToInsert['status'] = $id ? $data['status'][0] : 2;
        if ($id) {
            $this->db->where('id', $id);
            return $this->db->update("tbl_user", $dataToInsert);
        }
        return $this->db->insert("tbl_user", $dataToInsert);
    }
}
